import { Component, Output, Input, EventEmitter } from '@angular/core';
import { joinFile_API } from '@/api/files';
//导入公共依赖模块
import { ShareModule, NzMessageService } from '@app/share/share.module';
import { Router, Route, ActivatedRoute } from '@angular/router';
import { getStore } from '@/utils';
@Component({
  selector: 'InvitationDialog',
  template: `
    <nz-modal
      [(nzVisible)]="isVisible"
      nzTitle="邀请加入编辑"
      (nzOnCancel)="_cancel()"
      (nzOnOk)="_save()"
    >
      <ng-container *nzModalContent>
        <span class="username">{{ username }}</span>
        <span>
          邀请你加入 <a type="primary">{{ filename }}</a>
        </span>
      </ng-container>
    </nz-modal>
  `,
  standalone: true,
  imports: [ShareModule]
})
export class InvitationDialogComponent {
  isVisible: boolean = true;
  username: string = '';
  filename: string = '';
  fileid: string = '';

  constructor(
    private message: NzMessageService,
    private route: ActivatedRoute,
    private router: Router
  ) {}
  ngOnInit(): void {
    this.route.queryParams.subscribe(params => {
      this.username = params['username'];
      this.filename = params['filename'];
      this.fileid = params['fileid'];
    });
  }

  async getAsyncQueryParams() {
    return new Promise((resolve, reject) => {
      this.route.queryParams.subscribe(params => {
        resolve(params);
      });
    });
  }

  async _save() {
    try {
      const userInfo = getStore({ name: 'userInfo' }) || '';
      this.getAsyncQueryParams().then(async params => {
        const res = await joinFile_API(userInfo.userid, params['fileid']);
        if (res.code !== 200) this.message.create('error', res.msg);
        this.message.create('success', res.msg);
        this.router.navigate(['/']);
        this._cancel();
      });
    } catch (error) {
      console.log('error', error);
    }
  }

  _cancel() {
    this.isVisible = false;
  }
}
